Grokking Google Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Google Easy
Add Binary (easy)
Solution: Add Binary
Valid Mountain Array (easy)
Solution: Valid Mountain Array
Set Mismatch (easy)
Solution: Set Mismatch
Is Subsequence (easy)
Solution: Is Subsequence
Length of Last Word (easy)
Solution: Length of Last Word
Search Insert Position (easy)
Solution: Search Insert Position
Repeated Substring Pattern (easy)
Solution: Repeated Substring Pattern
Monotonic Array (easy)
Solution: Monotonic Array
Count Nodes Equal to Average of Subtree (easy)
Solution: Count Nodes Equal to Average of Subtree
Find the Difference (easy)
Solution: Find the Difference
How Many Numbers Are Smaller Than the Current Number (easy)
Solution: How Many Numbers Are Smaller Than the Current Number
Binary Tree Paths (easy)
Solution: Binary Tree Paths
Find the Difference of Two Arrays (easy)
Solution: Find the Difference of Two Arrays
Merge Strings Alternately (easy)
Solution: Merge Strings Alternately
Reverse a LinkedList (easy)
Solution: Reverse a LinkedList
Google Medium
Gray Code (medium)
Solution: Gray Code
2 Keys Keyboard (medium)
Solution: 2 Keys Keyboard
Arithmetic Slices (medium)
Solution: Arithmetic Slices
Unique Length-3 Palindromic Subsequences (medium)
Solution: Unique Length-3 Palindromic Subsequences
Minimum Difference Between Largest and Smallest Value in Three Moves (medium)
Solution: Minimum Difference Between Largest and Smallest Value in Three Moves
Letter Combinations of a Phone Number (medium)
Solution: Letter Combinations of a Phone Number
01 Matrix (medium)
Solution: 01 Matrix
Maximum Points You Can Obtain from Cards (medium)
Solution: Maximum Points You Can Obtain from Cards
Strobogrammatic Number II (medium)
Solution: Strobogrammatic Number II
Maximum Length of Semi-Decreasing Subarrays (medium)
Solution: Maximum Length of Semi-Decreasing Subarrays
Minimum Area Rectangle (medium)
Solution: Minimum Area Rectangle
Count Unreachable Pairs of Nodes in an Undirected Graph (medium)
Solution: Count Unreachable Pairs of Nodes in an Undirected Graph
Single Number II (medium)
Solution: Single Number II
4Sum II (medium)
Solution: 4Sum II
Furthest Building You Can Reach (medium)
Solution: Furthest Building You Can Reach
Minimizing Array After Replacing Pairs With Their Product (medium)
Solution: Minimizing Array After Replacing Pairs With Their Product
Fair Distribution of Cookies (medium)
Solution: Fair Distribution of Cookies
Find Leaves of Binary Tree (medium)
Solution: Find Leaves of Binary Tree
Rotate Image (medium)
Solution: Rotate Image
Combination Sum (medium)
Solution: Combination Sum
Implement Trie (Prefix Tree) (medium)
Solution: Implement Trie (Prefix Tree)
Number of Islands (medium)
Solution: Number of Islands
Edit Distance (medium)
Solution: Edit Distance
Container With Most Water (Medium)
Solution: Container With Most Water
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Shortest Subarray with Sum at Least K (hard)
Solution: Shortest Subarray with Sum at Least K
Reverse Pairs (hard)
Solution: Reverse Pairs
Nth Magical Number (hard)
Solution: Nth Magical Number
Candy (hard)
Solution: Candy
Arithmetic Slices II - Subsequence (hard)
Solution: Arithmetic Slices II - Subsequence
Burst Balloons (hard)
Solution: Burst Balloons
Sum of Distances in Tree (hard)
Solution: Sum of Distances in Tree
Maximum Score of a Good Subarray (hard)
Solution: Maximum Score of a Good Subarray
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Solution: 2 Keys Keyboard

Problem Statement

You're given a notepad that initially displays a single character 'A'. You have two actions available to perform on this notepad for each step:

  • Copy All: It allows you to copy everything on the screen.
  • Paste: You can paste the characters which are copied last time.

Given an integer n, return the minimum number of operations to print the character 'A' exactly n times on the screen.

Examples

  • Example 1:

    • Input: n = 4
    • Expected Output: 4
    • Justification: Start with 'A', copy it (1 operation), and paste it(1 operations). Now, you have 'AA'. Copy it, and paste it. Total = 4 operations.
  • Example 2:

    • Input: n = 5
    • Expected Output: 5
    • Justification: Start with 'A', copy it, and paste it four times. Total = 5 operations.
  • Example 3:

    • Input: n = 9
    • Expected Output: 6
    • Justification: Start with 'A', copy and paste twice (3 operations) to get 'AAA'. Copy 'AAA' and paste it twice (3 operations) to get 'AAAAAAAAA'. Total = 6 operations.

Solution

To solve this problem, we'll focus on breaking down the target number n into its prime factors, because the minimum number of operations needed to achieve n 'A's on the screen can be thought of in terms of multiplying the counts of 'A's by repeatedly copying and pasting. This strategy works because copying and pasting a larger block of 'A's, which is a multiple of smaller blocks, mirrors the process of constructing a number by multiplying its factors. Thus, our approach will be to find the sum of the prime factors of n as this sum represents the minimum number of steps needed to get n 'A's on the screen.

This approach is effective because it leverages the mathematical property that any composite number can be expressed as a product of prime numbers, and the operations to achieve the target n can be seen as the process of building up to n through these prime multiplicands. Therefore, by analyzing n in terms of its prime components, we can efficiently determine the least number of steps required.

Step-by-step algorithm

  • Initialize totalOperations to 0, which will hold our final count of operations.
  • Iterate from i = 2 to n:
    • While n % i == 0 (meaning i is a factor of n):
      • Add i to totalOperations (representing the operation count needed to achieve i copies).
      • Divide n by i to reduce our problem size and reflect that we've accounted for this factor.
  • Return totalOperations as the answer.

Algorithm Walkthrough

Let's consider the input: n = 9.

  1. Initialize operations to 0. This variable tracks the total number of operations required.
  2. Start iterating i from 2 to n (inclusive).
    • For n = 9, start with i = 2.
  3. Check if n % i == 0 (if n is divisible by i without a remainder).
    • For i = 2, 9 % 2 != 0. So, move to the next i.
    • For i = 3, 9 % 3 == 0. This is true, so enter the while loop.
  4. Inside the while loop, add i to operations. For the first iteration, add 3 to operations.
  5. Divide n by i, setting n to n / i. For the first iteration, set n = 9 / 3 = 3.
  6. Check again if n % i == 0. Since n is now 3 and i is also 3, the condition is true.
    • Add i (3) to operations again. Now, operations = 6.
    • Divide n by i again, now n = 3 / 3 = 1.
  7. Since n is now 1, the loop for i = 3 finishes.
  8. There are no more divisors of n (now 1) greater than 1, so the outer loop terminates.
  9. The value of operations is now 6, which is the expected minimum number of operations to get the character 'A' exactly n times on the screen for n = 9.

Code

Java
Saved to Cloud

. . . .

Complexity Analysis

Time Complexity

The time complexity of the provided code is in the worst case, primarily because it iterates through all numbers up to n and performs division operations that, in the worst case, can be as costly as iterating up to the square root of n for each number.

Space Complexity

The space complexity is , as it uses a fixed amount of space regardless of input size.

2 Keys Keyboard (medium)
Arithmetic Slices (medium)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-step algorithm

Algorithm Walkthrough

Code

Complexity Analysis

Time Complexity

Space Complexity